package com.xuelang.pipeline.infrastructure.pipeline.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuelang.pipeline.domain.pipeline.TriggerSetting;
import com.xuelang.pipeline.domain.pipeline.repository.TriggerSettingRepository;
import com.xuelang.pipeline.infrastructure.pipeline.converter.TriggerSettingToDOConverter;
import com.xuelang.pipeline.infrastructure.pipeline.dataobject.TriggerSettingDO;
import com.xuelang.pipeline.infrastructure.pipeline.mapper.TriggerSettingMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * <p>触发配置仓库实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-12 10:57
 **/
@Slf4j
@Repository
public class TriggerSettingRepositoryImpl implements TriggerSettingRepository {

    @Autowired
    private TriggerSettingMapper mapper;

    @Override
    public TriggerSetting queryByPipelineCode(String pipelineCode) {
        LambdaQueryWrapper<TriggerSettingDO> wrapper = Wrappers.lambdaQuery(TriggerSettingDO.class);
        wrapper.eq(StringUtils.isNotBlank(pipelineCode), TriggerSettingDO::getPipelineCode, pipelineCode);
        TriggerSettingDO found = mapper.selectOne(wrapper);
        if (Objects.nonNull(found)) {
            return TriggerSettingToDOConverter.INSTANCE.fromDO(found);
        }
        return null;
    }

    @Override
    public void add(TriggerSetting triggerSetting) {
        mapper.insert(TriggerSettingToDOConverter.INSTANCE.toDO(triggerSetting));
    }

    @Override
    public void update(TriggerSetting triggerSetting) {
        mapper.updateById(TriggerSettingToDOConverter.INSTANCE.toDO(triggerSetting));
    }

    @Override
    public void removeByIds(List<String> idList) {
        mapper.deleteBatchIds(idList);
    }

    @Override
    public void removeByCodes(List<String> codeList) {
        if(CollectionUtil.isNotEmpty(codeList)) {
            LambdaQueryWrapper<TriggerSettingDO> wrapper = Wrappers.lambdaQuery(TriggerSettingDO.class);
            wrapper.in(TriggerSettingDO::getPipelineCode, codeList);
            mapper.delete(wrapper);
        }
    }
}
